﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.UserControls;
using HuaCheng.CommonClass;

namespace HuaCheng.Views
{
    public partial class AuxCustom : Form
    {
        //保存当前主通道所勾选的辅助通道
        public static Dictionary<int, string> mapping = new Dictionary<int, string>();
        //离开的主通道号
        int leaveNum = 1;
        //当前点击的主通道号
        int nowNum ;
        //设备号
        public static string devIp;
        public delegate void showAuxChl();//委托通道映射页面显示辅助通道
        public event showAuxChl setAuxChl;//触发时间
        public AuxCustom(string ip)
        {
            InitializeComponent();
            devIp = ip;//设备Ip
            showAux(1);
        }
        public void onSetAuxChl()
        {
            if (setAuxChl != null)
            {
                setAuxChl();
            }
        }
        /// <summary>
        /// 按名称查找TreeView中的节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        //private TreeNode FindNode(TreeNode node, string name)
        //{
        //    TreeNode ret = null;
        //    foreach (TreeNode temp in node.Nodes)
        //    {
        //        if (temp.Nodes.Count != 0)
        //        {
        //            if ((ret = FindNode(temp, name)) != null)
        //            {
        //                return ret;
        //            }
        //        }
        //        if (string.Equals(temp.Text, name))
        //        {
        //            return temp;
        //        }
        //    }
        //    return ret;
        //}
        /// <summary>
        /// 显示当前所选主通道所映射的辅助通道
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //当前点击的节点号
            nowNum = Convert.ToInt32(e.Node.Name.Substring(4,1).ToString());
            //主通道realId
            int realId=nowNum-1;
            //判断是否运行中、停止、锁定等状态
            bool isrun = ChannelMap.channels[realId].LockedStat || ChannelMap.channels[realId].RunStat || ChannelMap.channels[realId].StopStat;
            //运行中、停止、锁定等状态不可以改变辅助通道映射
            if (isrun)
            {
                pnlAux.Enabled = false;
            }
            else
            {
                pnlAux.Enabled = true;
                //若当前主通道为撤销状态则不可以设置辅助通道
                if (ChannelMap.revokeChl.Contains(nowNum - 1))
                {
                    pnlAux.Enabled = false;
                }
                //如果当前主通道已设置过辅助通道映射则更新mapDic
                if (ChannelMap.mapDic.ContainsKey(leaveNum))
                {
                    ChannelMap.mapDic[leaveNum] = GetChecked();
                }
                //如果当前主通道未设置过辅助通道映射则添加mapDic
                else
                {
                    ChannelMap.mapDic.Add(leaveNum, GetChecked());
                }
                //显示当前点击的主通道的映射信息
                showAux(nowNum);
                leaveNum = nowNum;
            }
        }
        /// <summary>
        /// 获取勾选的辅助通道
        /// </summary>
        /// <param name="clb"></param>
        /// <returns></returns>
        private string GetChecked()
        {
            //用于保存映射的辅助通道
            List<String> str = new List<string>();
            //遍历辅助通道checkBox
            foreach (Control cbxControl in this.pnlAux.Controls)
            {
                if (cbxControl is CheckBox)
                {
                    CheckBox cbxAux = (CheckBox)this.pnlAux.Controls[cbxControl.Name];
                    //若打钩则添加被映射的辅助通道号
                    if (cbxAux.Checked == true)
                    {
                        str.Add(cbxAux.Name.Substring(6, 1));
                    }
                }
            }  
            return string.Join(",", str);
        }
        /// <summary>
        /// 将当前点击的主通道映射的辅助通道显示，并将其他主通道映射过的辅助通道变为不可选
        /// </summary>
        /// <param name="strMain"></param>
        private void showAux(int strMain)
        {
            clbAux_ClearChecked();
            //将当前点击的主通道映射的辅助通道显示
            if (ChannelMap.mapDic.ContainsKey(strMain))
            {
                String str = ChannelMap.mapDic[strMain];
                if (str != "")
                {
                    List<String> checkedAuxNum = str.Split(',').ToList();
                    String checkedAuxNumName;
                    foreach(String strNum in checkedAuxNum){
                        if (strNum!="")
                        {
                            checkedAuxNumName = "cbxAux" + strNum;
                            CheckBox cbxAux = (CheckBox)this.pnlAux.Controls[checkedAuxNumName];
                            cbxAux.Checked = true;
                        }
                        
                    }
                }
            }
            //将其他主通道映射过的辅助通道变为不可选
            foreach (int key in ChannelMap.mapDic.Keys)
            {
                if (key != strMain)
                {
                    String value = ChannelMap.mapDic[key];
                    if (value != "")
                    {
                        List<String> checkedAuxNum = value.Split(',').ToList();
                        String checkedAuxNumName;
                        foreach (String strNum in checkedAuxNum)
                        {
                            if(strNum!="")
                            {
                                checkedAuxNumName = "cbxAux" + strNum;
                                CheckBox cbxAux = (CheckBox)this.pnlAux.Controls[checkedAuxNumName];
                                cbxAux.Enabled = false;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 将辅助通道的勾选项取消勾选,不可用项变为可用
        /// </summary>
        private void clbAux_ClearChecked()
        {
            foreach (Control cbxControl in this.pnlAux.Controls)
            {
                if (cbxControl is CheckBox)
                {
                    CheckBox cbxAux = (CheckBox)this.pnlAux.Controls[cbxControl.Name];
                    //将勾选的改为未勾选
                    if (cbxAux.Checked == true)
                    {
                        cbxAux.Checked = false;
                    }
                    //将不可用的改为可用
                    if (cbxAux.Enabled == false)
                    {
                        cbxAux.Enabled = true;
                    }
                }
            }  
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 辅助通道映射后点击确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnYes_Click(object sender, EventArgs e)
        {
            //最后一次选择完辅助通道后直接点击确定按钮
            if (ChannelMap.mapDic.ContainsKey(nowNum))
            {
                ChannelMap.mapDic[nowNum] = GetChecked();
            }
            else
            {
                ChannelMap.mapDic.Add(nowNum, GetChecked());
            }
            //OnAuxNumsSave(mapping);
            onSetAuxChl();
            this.Close();
        }

        #region 国际化
        private void AuxCustom_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("SetAuxChlMap");
            for (int i = 0; i < tvMain.Nodes.Count; i++)
            {
                this.tvMain.Nodes[i].Text = ResourceCulture.GetString("Main") + (i+1).ToString();
            }
            this.cbxAux1.Text = ResourceCulture.GetString("AuxChannel") + "1";
            this.cbxAux2.Text = ResourceCulture.GetString("AuxChannel") + "2";
            this.cbxAux3.Text = ResourceCulture.GetString("AuxChannel") + "3";
            this.cbxAux4.Text = ResourceCulture.GetString("AuxChannel") + "4";
            this.cbxAux5.Text = ResourceCulture.GetString("AuxChannel") + "5";
            this.cbxAux6.Text = ResourceCulture.GetString("AuxChannel") + "6";
            this.cbxAux7.Text = ResourceCulture.GetString("AuxChannel") + "7";
            this.cbxAux8.Text = ResourceCulture.GetString("AuxChannel") + "8";
            this.btnCancel.Text = ResourceCulture.GetString("LoginCancel");
            this.btnYes.Text = ResourceCulture.GetString("LoginOk");
        }
        #endregion
    }
}
